package regex;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TestKseParser {
	private static Pattern ksePattern = Pattern
			.compile("<kse:widget\\s+data-widget-type=\"widget\"(?:\\s+id=\"?(\\d+)\"?|\\s+type=\"([\\w\\s]+)\"|\\s+title=\"([\\w\\s]+)\"){3}\\s*>(.*)?</kse:widget>");

	public static void main(String[] args) throws IOException {
		long start = System.currentTimeMillis();
		int count = 0;
		int t = 1;
		for (int i = 0; i < t; i++) {
			count = testKseParse();
		}
		long end = System.currentTimeMillis();
		System.out.println(String.format("count:%s,time:%s", count,
				(end - start) * 1.0 / t));
	}

	private static int testKseParse() throws IOException {
		byte[] byts = new byte[1024 * 1024];
		TestKseParser.class.getResourceAsStream("testKse").read(byts);
		String testKse = new String(byts);
		// System.out.println(testKse);
		Matcher matcher = ksePattern.matcher(testKse);
		int count = 0;
		while (matcher.find()) {
			count++;
			String fitered = matcher.replaceAll("$");
			System.out.println("fitered:" + fitered);
			String id = matcher.group(1);
			String type = matcher.group(2);
			String title = matcher.group(3);
			System.out.println(String.format("kse:[id=%s,type=%s,title=%s]",
					id, type, title));
		}
		return count;
	}
}
